package net.lainiao.service.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import net.lainiao.base.model.ShiroRole;
import net.lainiao.base.model.ShiroUser;
import net.lainiao.service.dao.ShiroRoleMapper;
import net.lainiao.service.dao.ShiroUserMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/1/19.
 */
@Service
public class ShiroUserService {
    @Resource
    private ShiroUserMapper shiroUserMapper;
    @Resource
    private ShiroRoleMapper shiroRoleMapper;

    public ShiroUser getUserByUserName(String username) {
        return shiroUserMapper.getUserByUserName(username);
    }

    public ShiroUser login(String username, String password) {
        return shiroUserMapper.login(username, password);
    }


    public int save(ShiroUser user) {
        if(user.getId()==null){
            return shiroUserMapper.insert(user);
        }
        return shiroUserMapper.updateByPrimaryKey(user);
    }

    public int del(int id) {
        return shiroUserMapper.deleteByPrimaryKey(id);
    }

    public PageInfo<ShiroUser> getModelsForPage(int pageIndex) {
        PageHelper.startPage(pageIndex,10);
        List<ShiroUser> shiroUsers=shiroUserMapper.getAll();
        PageInfo<ShiroUser> pageInfo=new PageInfo<>(shiroUsers);
        return pageInfo;
    }

    public List<ShiroRole> getRolesForUserId(int userId) {
        return shiroRoleMapper.getRolesForUserId(userId);
    }

    public ShiroUser getModelForId(int userId) {
        return shiroUserMapper.selectByPrimaryKey(userId);
    }
}
